/**
 * Copyright 2010 Terrestrial Ecosystem Research Network, licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
 * agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package au.org.tern.ecoinformatics.oai.provider.sample.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import au.org.tern.ecoinformatics.oai.provider.sample.dao.SampleDatasetDao;
import au.org.tern.ecoinformatics.oai.provider.sample.model.SampleDataset;

/**
 * This example simply serves up newly created objects. Obviously a real implementation will 
 * perform a lookup of persistent data, either from file system or database.
 * 
 * @author vhobbs
 *
 */
public class SampleDatasetDaoImpl implements SampleDatasetDao {

	private List<SampleDataset> datasets = null;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public SampleDataset get(Long id) {

		return getDatasets(null, null).get(id.intValue() - 1);
	}

	
	@Override
	public List<SampleDataset> getDatasets(Date from, Date until) {
		if (datasets == null) {
			createSampleDatasets();
		}
		
		return datasets;
	}

	
	private void createSampleDatasets() {
		datasets = new ArrayList<SampleDataset>();

		SampleDataset dataset1 = new SampleDataset();
		dataset1.setId(1L);
		dataset1.setTitle("Dataset 1");
		dataset1.setAbstrakt("Dataset 1 is a sample dataset");
		dataset1.setDescription("Dataset 1 is a sample dataset to illustrate "
				+ "how a persistent enterprise object can be retrieved from a database "
				+ "and made available for OAI harvesting.");
		dataset1.setPurpose("sample dataset");

		dataset1.setCreatorName("Prof. Joe Bloggs");
		dataset1.setCreatorEmail("joe.bloggs@institution.edu.au");
		dataset1.setPublisherName("Dept. Computer Science");
		dataset1.setPublisherEmail("publisher@institution.edu.au");
		try {
			dataset1.setStartDate(dateFormat.parse("2008-01-01"));
			dataset1.setEndDate(dateFormat.parse("2008-12-31"));
			dataset1.setCreationDate(dateFormat.parse("2009-01-31"));
			dataset1.setModificationDate(dateFormat.parse("2009-01-31"));
		} 
		catch (ParseException pe) {
			throw new RuntimeException(pe);
		}
		
		dataset1.setNorthernExtent(-23.0);
		dataset1.setSouthernExtent(-28.0);
		dataset1.setEasternExtent(142.0);
		dataset1.setWesternExtent(136.0);
		datasets.add(dataset1);

		SampleDataset dataset2 = new SampleDataset();
		dataset2.setId(2L);
		dataset2.setTitle("Dataset 2");
		dataset2.setAbstrakt("Dataset 2 is a sample dataset");
		dataset2.setDescription("Dataset 2 is a sample dataset to illustrate "
				+ "how a persistent enterprise object can be retrieved from a database "
				+ "and made available for OAI harvesting.");
		dataset2.setPurpose("sample dataset");

		dataset2.setCreatorName("Prof. Jim Bloggs");
		dataset2.setCreatorEmail("jim.bloggs@institution.edu.au");
		dataset2.setPublisherName("Dept. Computer Science");
		dataset2.setPublisherEmail("publisher@institution.edu.au");
		try {
			dataset2.setStartDate(dateFormat.parse("2009-01-01"));
			dataset2.setEndDate(dateFormat.parse("2009-12-31"));
			dataset2.setCreationDate(dateFormat.parse("2010-01-31"));
			dataset2.setModificationDate(dateFormat.parse("2010-01-31"));
		} 
		catch (ParseException pe) {
			throw new RuntimeException(pe);
		}
		
		dataset2.setNorthernExtent(-23.0);
		dataset2.setSouthernExtent(-28.0);
		dataset2.setEasternExtent(142.0);
		dataset2.setWesternExtent(136.0);

		datasets.add(dataset2);
	}
}
